package com.car.dao.impl;

import com.car.dao.PurchaseRecordDao;
import com.car.doamin.PurchaseRecord;
import com.car.utils.JDBCUtil;

import java.util.List;


public class PurchaseRecordDaoImpl implements PurchaseRecordDao {

    /**
     * 新增
     *
     * @param purchaseRecord 实体类对象
     * @return 影响记录数
     */
    @Override
    public int add(PurchaseRecord purchaseRecord) throws Exception {
        String sql = "insert into t_purchase_record (car_id, user_id, employee_id, purchase_datetime) \n " + " VALUE(?,?,?,?)";
        return JDBCUtil.update(sql, purchaseRecord.getCardId(),purchaseRecord.getUserId(),  purchaseRecord.getEmployeeId(), purchaseRecord.getpurchaseDatetime());
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return 影响记录数
     */
    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from t_purchase_record where id = ? ";
        return JDBCUtil.update(sql, id);
    }

    /**
     * 更改
     *
     * @param purchaseRecord 实体类对象
     * @return 影响记录数
     */
    @Override
    public int update(PurchaseRecord purchaseRecord) throws Exception {
        String sql = "update t_purchase_record set car_id=?, user_id = ?, employee_id=?, purchase_datetime=? \n " + "where id = ?";
        return JDBCUtil.update(sql, purchaseRecord.getCardId(), purchaseRecord.getUserId(),  purchaseRecord.getEmployeeId(), purchaseRecord.getpurchaseDatetime(), purchaseRecord.getId());
    }

    /**
     * 查询
     *
     * @param id 主键id
     * @return 影响记录数
     */
    @Override
    public PurchaseRecord queryById(int id) throws Exception {
        String sql = "select * from t_purchase_record where id = ?";
        return JDBCUtil.query(sql, PurchaseRecord.class, id);
    }

    /**
     * 查询所有数据
     *
     * @return 查询到的对象
     */
    @Override
    public List<PurchaseRecord> queryAll() throws Exception {
        String sql = "select * from t_purchase_record";
        return JDBCUtil.queryList(sql, PurchaseRecord.class);
    }

    /**
     * @param userId
     * @return
     */
    @Override
    public List<PurchaseRecord> findByUserId(Integer userId) throws Exception {
        String sql = "select * from t_purchase_record where user_id = ?";
        return JDBCUtil.queryList(sql,PurchaseRecord.class,userId);
    }
}
